﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NBusiness.Query;
using System.Reflection;

namespace NBusiness.Data.Testing.Mocks
{
    class MockModel : ModelBase
    {
        public static class Database
        {
            static Database()
            {
                IDatabaseProvider provider = (IDatabaseProvider)Settings.Instance.GetSetting(SettingsType.Provider, typeof(MockModel));
                _table = provider.Table("Mock");
                _id = _table.Column("Id");
                _name = _table.Column("Name");
            }

            private static ITable _table;
            private static IColumn _id;
            private static IColumn _name;

            public static ITable Table { get { return _table; } }
            public static IColumn Id { get { return _id; } }
            public static IColumn Name { get { return _name; } }

            public static IColumn[] Columns { get { return new IColumn[] { Id, Name }; } }
        }

        private int _id;
        public int Id
        {
            get 
            {
                VerifyCanGet("Id");
                return _id; 
            }
            set
            {
                VerifyCanSet("Id");
                if (_id != value)
                {
                    _id = value;
                    OnPropertyChanged("Id");
                }
            }
        }
        private string _name;
        public string Name
        {
            get
            {
                VerifyCanGet("Name");
                return _name;
            }
            set
            {
                VerifyCanSet("Name");
                if (_name != value)
                {
                    _name = value;
                    OnPropertyChanged("Name");
                }
            }
        }

        public static void Fetch(int id, Action<MockModel> completed)
        {
            MockModel model = new MockModel();
            IQuery query = model.Provider.Query();
            query.Select().From(MockModel.Database.Table).Where(MockModel.Database.Id.Equal(id));
            query.Load += model.Load;
            model.Provider.Execute(query);

            completed(model);
        }

        protected override void InsertModel(IQuery query)
        {
            IInsert insert = query.Insert().Into(MockModel.Database.Table, MockModel.Database.Name).Values("@Name");
            query.Go();
            insert.Parameter("@Id").Direction = ParameterValueDirection.Output;
            insert.Parameter("@Name").Value = Name;
            query.Load += loader =>
            {
                Id = (int)insert.Parameter("@Id").Value;
            };
        }

        protected override void UpdateModel(IQuery query)
        {
            query.Update()
                .Table(MockModel.Database.Table)
                .Set(MockModel.Database.Name, "@Name")
                .Where(MockModel.Database.Id.Equal("@Id"));
        }

        protected override void DeleteModel(IQuery query)
        {
            throw new NotImplementedException();
        }

        protected override void ValidateFields()
        {
            MethodCall.Calls.Add(new MethodCall("ValidateFields"));
        }

        protected override void LoadModel(ILoader loader)
        {
            MethodCall.Calls.Add(new MethodCall(
                "LoadModel",
                new { Name = "loader", Value = loader }));
        }

        protected override void InitializeActions()
        {
            throw new NotImplementedException();
        }
    }
}
